﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace CLEXA.DataContracts
{
    [DataContract]
    public class UserData
    {
        private static readonly IEqualityComparer<UserData> valueComparer = new UserDataValueComparer();
        [DataMember]
        private readonly int userId;
        [DataMember]
        private readonly string username;
        [DataMember]
        private readonly bool isActive;

        public static IEqualityComparer<UserData> ValueComparer
        {
            get { return UserData.valueComparer; }
        } 

        public int UserId
        {
            get { return userId; }
        }

        public string Username
        {
            get { return username; }
        } 

        public bool IsActive
        {
            get { return isActive; }
        }

        private UserData()
        {

        }

        public UserData(int userId, string username, bool isActive)
        {
            this.userId = userId;
            this.username = username;
            this.isActive = isActive;
        }

        public override string ToString()
        {
            return string.Format("UserId: {0}, Username: {1}, IsActive: {2}", userId, username, isActive);
        }

        private sealed class UserDataValueComparer : IEqualityComparer<UserData>
        {
            public bool Equals(UserData x, UserData y)
            {
                if (Object.ReferenceEquals(x, y))
                {
                    return true;
                }
                if (Object.ReferenceEquals(x, null))
                {
                    return false;
                }
                if (Object.ReferenceEquals(y, null))
                {
                    return false;
                }

                return x.userId == y.userId && x.username == y.username && x.isActive == y.isActive;
            }

            public int GetHashCode(UserData obj)
            {
                if (obj == null)
                {
                    return 0;
                }
                else
                {
                    return obj.userId.GetHashCode() ^ obj.username.GetHashCode() ^ obj.isActive.GetHashCode();
                }
            }
        }
    }
}
